﻿using FunctionalCLR.Core;
//using FunctionalCLR.Core.Logging;
using LibUnitConverter;
using System.Configuration;

namespace Zove.Runtime {
    public class ZoveDependencyManager {

        private static ZoveDependencyManager dm = new ZoveDependencyManager();

        private IConstantManager constantManager = null;

        private IFunctionManager functionManager = null;

        private IExpressionCompiler expressionCompiler = null;

        private IParserFactory parserFactory = null;

        private IClassFinder classFinder = null;

        private IClassLoader classLoader = null;

        private IRuntimeModelLoader runtimeModelLoader = null;

        private IUnitConverter unitConverter = null;

        private ZoveDependencyManager() {
        }

        public static ZoveDependencyManager Instance {
            get {
                return dm;
            }
        }

        public IConstantManager ConstantManager {
            get {
                if (this.constantManager == null) {
                    this.constantManager = new DefaultConstantManager(Utils.ConstantFile);
                }

                return this.constantManager;
            }
            set {
                this.constantManager = value;
            }
        }

        public IFunctionManager FunctionManager {
            get {
                if (this.functionManager == null) {
                    this.functionManager = new ZoveFunctionManager();
                }

                return this.functionManager;
            }
            set {
                this.functionManager = value;
            }
        }

        public IExpressionCompiler ExpressionCompiler {
            get {
                if (this.expressionCompiler == null) {
                    this.expressionCompiler = new DefaultExpressionCompiler();
                }

                return this.expressionCompiler;
            }
            set {
                this.expressionCompiler = value;
            }
        }

        public IClassFinder ClassFinder {
            get {
                if (this.classFinder == null) {
                    this.classFinder = new DefaultClassFinder();
                }
                return this.classFinder;
            }
            set {
                this.classFinder = value;
            }
        }

        public IRuntimeModelLoader RuntimeModelLoader {
            get {
                if (this.runtimeModelLoader == null) {
                    this.runtimeModelLoader = new DefaultRuntimeModelLoader();
                    //((DefaultRuntimeModelLoader)this.runtimeModelLoader).Logger = new DefaultLogger();
                    ((DefaultRuntimeModelLoader)this.runtimeModelLoader).ClassLoader = this.ClassLoader;
                }

                return this.runtimeModelLoader;
            }
            set {
                this.runtimeModelLoader = value;
            }
        }

        public IClassLoader ClassLoader {
            get {
                if (this.classLoader == null) {
                    this.classLoader = new DefaultClassLoader();
                    ((DefaultClassLoader)this.classLoader).Finder = this.ClassFinder;
                }

                return this.classLoader;
            }
            set {
                this.classLoader = value;
            }
        }

        public IParserFactory ParserFactory {
            get {
                if (this.parserFactory == null) {
                    this.parserFactory = new ZoveParserFactory();
                }
                return this.parserFactory;
            }
        }

        public IUnitConverter UnitConverter {
            get {
                if (this.unitConverter == null) {
                    this.unitConverter = new DefaultUnitConverter();
                }

                return this.unitConverter;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string GetFileFile(string p) {
            string filePath = ConfigurationManager.AppSettings["help"];
            filePath = filePath.EndsWith("\\") ? filePath : filePath + "\\";
            filePath = filePath + p + ".html";

            return filePath;
        }
    }
}
